package com.java.controller;

import com.java.bean.*;
import com.java.mvc.ResponseBody;
import com.java.service.CourierService;
import com.java.util.DateFormatUtil;
import com.java.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CourierController {
    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String, Integer>> data = CourierService.console();
        Message msg = new Message();
        if(data.size() == 0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }


    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        //1.    获取查询数据的起始索引值
        int offset = Integer.parseInt(request.getParameter("offset"));
        //2.    获取当前页要查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //3.    进行查询
        List<Courier> list = CourierService.findAll(true, offset, pageNumber);
        List<BootStrapTableCourier> list2 = new ArrayList<>();
        for(Courier e:list){
            String createTime = DateFormatUtil.format(e.getCreateTime());
            String loginTime = DateFormatUtil.format(e.getLoginTime());
            BootStrapTableCourier e2 = new BootStrapTableCourier(e.getId(),e.getName(),e.getPhone(),e.getID_Card(),e.getPassword(),e.getNumberOfPackages(),createTime,loginTime);
            list2.add(e2);
        }
        List<Map<String, Integer>> console = CourierService.console();
        Integer total = console.get(0).get("courier_size");
        //4.    将集合封装为 bootstrap-table识别的格式
        ResultData<BootStrapTableCourier> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/courier/find.do")
    public String find(HttpServletRequest request,HttpServletResponse response){
        String phone = request.getParameter("phone");
        Courier e = CourierService.findByNumber(phone);
        Message msg = new Message();
        if(e == null){
            msg.setStatus(-1);
            msg.setResult("手机号不存在");
        }else{
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(e);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String ID_Card = request.getParameter("ID_Card");
        String password = request.getParameter("password");
        //Express e = new Express(number,company,username,userPhone, UserUtil.getUserPhone(request.getSession()));
        Courier e = new Courier(name,phone,ID_Card,password);
        boolean flag = CourierService.insert(e);
        Message msg = new Message();
        if(flag){
            //录入成功
            msg.setStatus(0);
            msg.setResult("快递员录入成功!");
        }else{
            //录入失败
            msg.setStatus(-1);
            msg.setResult("快递员录入失败!");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }



    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request,HttpServletResponse response){
        String password = request.getParameter("password");
        String ID_Card = request.getParameter("ID_Card");
        String name = request.getParameter("name");
        String old_phone = request.getParameter("phone");
        String new_phone = request.getParameter("new_phone");
        Courier newCourier = new Courier();
        newCourier.setPassword(password);
        newCourier.setID_Card(ID_Card);
        newCourier.setName(name);
        newCourier.setPhone(old_phone);
        boolean flag = CourierService.update(new_phone, newCourier);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request,HttpServletResponse response){
        String phone = request.getParameter("phone");
        boolean flag = CourierService.delete(phone);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}
